package com.aice.chinamapview.slice;

import com.aice.chinamapview.ResourceTable;
import com.aice.chinamapview.component.ArrowRefreshHeader;
import com.aice.chinamapview.component.CoordinatorBottomLayout;
import com.aice.chinamapview.component.CoordinatorLayout;
import com.aice.chinamapview.component.HmButton;
import com.noteam.chinamapview.ChinaMapView;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.ListContainer;
import ohos.agp.components.StackLayout;
import ohos.app.Context;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.multimodalinput.event.TouchEvent;

public class SwipRefreshAppbarActivitySlice extends AbilitySlice implements Component.TouchEventListener,
        Component.ClickedListener {
    private Context mContext;
    private StackLayout stackLayout;
    private int initMarginTop = 1290;
    private CoordinatorLayout coordinatorLayout;
    private CoordinatorBottomLayout coordinatorBottomLayout;
    private ArrowRefreshHeader mRefreshHeader;
    public int marginTop;
    private int pointDownY;
    private int pointMove;
    private int mLastY = -1;
    private boolean refreshEnabled = false;
    private float dragRate = 3;
    private int pointMove1;
    private HmButton btnChange;
    private ChinaMapView chinaMapView;
    private ListContainer listContainer;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_swip_refresh_appbar_activity);
        initView();
    }

    private void initView() {
        mContext = this;
        stackLayout = (StackLayout) findComponentById(ResourceTable.Id_stackLayout);
        stackLayout.setClickable(false);
        coordinatorLayout = new CoordinatorLayout(this);
        coordinatorLayout.setLayoutConfig(new DirectionalLayout.
                LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT));
        coordinatorLayout.setOrientation(Component.VERTICAL);
        stackLayout.addComponent(coordinatorLayout);
        stackLayout.setTouchEventListener(this::onTouchEvent);
        coordinatorBottomLayout = new CoordinatorBottomLayout(this);
        coordinatorBottomLayout.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT));
        coordinatorBottomLayout.setOrientation(Component.VERTICAL);
        stackLayout.addComponent(coordinatorBottomLayout);
        listContainer = (ListContainer) coordinatorBottomLayout.findComponentById(ResourceTable.Id_listcontainer);
        chinaMapView = (ChinaMapView) coordinatorLayout.findComponentById(ResourceTable.Id_mapView);
        mRefreshHeader = new ArrowRefreshHeader(this);
        mRefreshHeader.setLayoutConfig(new DirectionalLayout.LayoutConfig(DirectionalLayout.LayoutConfig.MATCH_PARENT,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT));
        stackLayout.addComponent(mRefreshHeader);
        this.getMainTaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                marginTop = initMarginTop;
                coordinatorBottomLayout.setMarginTop(initMarginTop);
            }
        });
        coordinatorLayout.setLoadingListener(new CoordinatorLayout.LoadingListener() {
            @Override
            public void onRefresh() {
                TaskDispatcher globalTaskDispatcher = getUITaskDispatcher();
                globalTaskDispatcher.delayDispatch(() -> {
                    coordinatorLayout.refreshData();
                    if (mRefreshHeader != null) {
                        mRefreshHeader.refreshComplete();
                    }
                }, 3000);
            }

        });
        btnChange = (HmButton) coordinatorLayout.findComponentById(ResourceTable.Id_btnChange);
        btnChange.setClickedListener(this::onClick);

        chinaMapView.setOnProvinceSelectedListener(new ChinaMapView.OnProvinceSelectedListener() {
            @Override
            public void onProvinceSelected(ChinaMapView.Area pArea, boolean repeatClick) {
                coordinatorBottomLayout.notifyData(pArea.nameValue);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    private float getTouchY(TouchEvent touchEvent, int index, Component component) {
        int touchY = 0;
        if (touchEvent.getPointerCount() > index) {
            int[] xy = component.getLocationOnScreen();
            if (xy != null && xy.length == 2) {
                touchY = (int)touchEvent.getPointerScreenPosition(index).getY() - xy[1];
            } else {
                touchY = (int)touchEvent.getPointerPosition(index).getY();
            }
        }
        return touchY;
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        boolean ia = listContainer.canScroll(Component.DRAG_UP);
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                pointDownY = (int)getTouchY(touchEvent, 0, component);
                mLastY = (int)touchEvent.getPointerPosition(0).getY() + component.getLocationOnScreen()[1];
                return true;
            case TouchEvent.POINT_MOVE:
                if (ia) {
                    int deltaY = (int)touchEvent.getPointerPosition(touchEvent.getIndex()).getY() - mLastY;
                    int movePoint = (int)getTouchY(touchEvent, touchEvent.getIndex(), component);
                    pointMove = pointDownY - movePoint + pointMove1;
                    if (pointMove < 0) {
                        refreshEnabled = true;
                        if (mRefreshHeader == null) {
                            break;
                        }
                        mRefreshHeader.onMove(deltaY / dragRate);
                        if (mRefreshHeader.getVisibleHeight() > 0 && mRefreshHeader.getState() < ArrowRefreshHeader.STATE_REFRESHING) {
                            return true;
                        }
                    } else {
                        mContext.getUITaskDispatcher().asyncDispatch(new Runnable() {
                            @Override
                            public void run() {
                                System.out.println("POINT_MOVE marginTop222------" + (marginTop - (int) (pointMove)));
                                    if (pointMove <= marginTop) {
                                        listContainer.scrollTo(0);
                                        if (pointMove > 10){
                                            listContainer.setEnabled(false);
                                        }
                                        coordinatorBottomLayout.setMarginTop(marginTop - (int) (pointMove));
                                        coordinatorLayout.setMarginTop(-(int) (pointMove * 0.75));
                                        System.out.println("POINT_MOVE marginTop1------");
                                    } else {
                                        listContainer.setEnabled(true);
                                        coordinatorBottomLayout.setMarginTop(0);
                                        coordinatorLayout.setMarginTop(-initMarginTop);
                                        System.out.println("POINT_MOVE marginTop2------");
                                    }
                                }
                        });
                    }
                    return true;
                }
                return true;
            case TouchEvent.PRIMARY_POINT_UP:
                if (refreshEnabled) {
                    refreshEnabled = false;
                    if (mRefreshHeader != null && mRefreshHeader.releaseAction()) {
                        if (coordinatorLayout.getLoadingListener() != null) {
                            coordinatorLayout.refresh();
                        }
                    }
                    pointMove1 = 0;

                } else {
                    pointMove1 = pointMove;
                }
                return true;
        }
        return true;
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btnChange:
                System.out.println("onClick----");
                coordinatorLayout.setMapData();
                break;
        }
    }
}
